package nuc.soft.xsf.controller;


import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import nuc.soft.xsf.model.Post;
import nuc.soft.xsf.model.PostSend;
import nuc.soft.xsf.model.User;
import nuc.soft.xsf.service.IPostSendService;
import nuc.soft.xsf.service.IPostService;
import nuc.soft.xsf.service.IUserService;
import nuc.soft.xsf.tools.Tools;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/PostSendController")
public class PostSendController {
	@Resource
	private IPostSendService postSendService;

	@Resource
	private IPostService postService;

	@Resource
	private IUserService userService;
	@RequestMapping(value = "/add.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addPost(Post post,HttpServletRequest request) {
		// 获取插入的帖子ID
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		Date now = new Date(); 
		String data=df.format(new Date());
		post.setReceiveTime(data);
		post.setDeleteState(0);
		int postId = postService.insert(post);
		PostSend postSend = new PostSend();
		postSend.setPostId(postId);
		int userId=(int) request.getSession().getAttribute("UserId"); 
		postSend.setUserId(userId);
		postSend.setDeleteState(0);
		// 插入到已发送帖子的表中
		postSendService.insert(postSend);
		/* PostSend postSend */
		JSONObject json = new JSONObject();
		json.put("post", post);
		return json.toString();

	}

	@RequestMapping(value = "/findByCondition.action", produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String findByCondition(PostSend postSend) {
		Map map=Tools.transBean2Map(postSend);
		List<PostSend> postList=postSendService.findByCondition(map);
		JSONObject json=new JSONObject();
		JSONArray json1=new JSONArray();
		for(int i=0;i<postList.size();i++){
			User u=userService.findById(postList.get(i).getUserId());
			json.put("uname", u.getUserName());
			Post post=postService.findById(postList.get(i).getPostId());
			json.put("title",post.getPostTitle());
			json.put("content",post.getPostContent());
			json.put("receiveTime", post.getReceiveTime());
			json.put("postId", post.getPostId());
			json.put("id",postList.get(i).getId());
			json1.add(json);
		}
		
		return json1.toString();
   }

	
	@RequestMapping(value = "/findByConditionfront.action", produces = "text/html;charset=UTF-8")
	public String findByConditionfront(PostSend postSend) {
		Map map=Tools.transBean2Map(postSend);
		List<PostSend> postList=postSendService.findByCondition(map);
		JSONObject json=new JSONObject();
		JSONArray json1=new JSONArray();
		for(int i=0;i<postList.size();i++){
			User u=userService.findById(postList.get(i).getUserId());
			json.put("uname", u.getUserName());
			Post post=postService.findById(postList.get(i).getPostId());
			json.put("title",post.getPostTitle());
			json.put("content",post.getPostContent());
			json.put("receiveTime", post.getReceiveTime());
			json.put("postId", post.getPostId());
			json.put("id",postList.get(i).getId());
			json1.add(json);
		}
		
		return "redirect:/MenuController/postListfront.action";
   }
	
	@RequestMapping(value = "/delete.action", produces = "text/html;charset=utf-8")
	@ResponseBody
	public String deleteTitle(String ids) {
		String[] str = ids.split(",");
		JSONObject json = new JSONObject();
		int success = 0;
		for (String strTmp : str) {
			PostSend postSend = postSendService.findById(Integer.parseInt(strTmp));
			if (postSend != null) {
               postSend.setDeleteState(1);
			   postSendService.update(postSend);
			   success++;
			}
		}
		json.put("success", success);
		return json.toString();
	}

}
